﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections.Generic;

namespace HRM
{
    public partial class frmBirthDayList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int department_ID = 0;
        int branch_ID = 0;
        MainServiceClient wcfMain;

        EmployeeClient wcfEmployee;
        RoleRightInfo listRole;
        public frmBirthDayList()
        {
            InitializeComponent();
            LoadLanguage();

            wcfEmployee = Service.Employee();
            wcfMain = Service.MainService();
            BuildVoucherGrid();
            UISetting.LoadMonths(ref cboMonth);
            listRole = UIParamater.Quyen_CanhBaoSapSinhNhat;

            if (!listRole.Update)
            {
                this.btnViewEmloyee.Image = global::HRM.Properties.Resources.app;
                this.btnViewEmloyee.Tag = "xem";
            }

            cboMonth.Text = DateTime.Now.Month.ToString();

            UISetting.LoadTreeView3(ref tvBranch);
            this.KeyPreview = true;

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
            //UISetting.AddColumn(ref dgvList, "Employee_Code", UpdateUI.GetString("manhanvien"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 200, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_DateOfBirth", UpdateUI.GetString("ngaysinh"), "", 110, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_PlaceOfBirth", UpdateUI.GetString("noisinh"), "", 130, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_Sex", UpdateUI.GetString("gioitinh"), "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_Age", UpdateUI.GetString("tuoi"), "C", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Branch_Name", UpdateUI.GetString("chinhanh"), "L", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Department_Name", UpdateUI.GetString("phongban"), "L", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Position_Name", UpdateUI.GetString("chucvu"), "L", 100, true, true);
        }
        private void LoadData()
        {
            int _month = Convert.ToInt32(cboMonth.Text);

            DataSet _ds = wcfEmployee.Employee_BirthDay(out itemCount, _month, branch_ID, department_ID, currPage, pageSize);

            PageIndex();

            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["Employee_ID"].Value = _ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                //dgvList.Rows[i].Cells["Employee_Code"].Value = _ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                dgvList.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_LastName"].ToString()
                    + " " + _ds.Tables[0].Rows[i]["Employee_FirstName"].ToString();
                if (Convert.ToBoolean(_ds.Tables[0].Rows[i]["Employee_Sex"]))
                    dgvList.Rows[i].Cells["Employee_Sex"].Value = "Nam";
                else
                    dgvList.Rows[i].Cells["Employee_Sex"].Value = "Nữ";
                dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["Employee_Age"].Value = _ds.Tables[0].Rows[i]["Employee_Age"].ToString();
                dgvList.Rows[i].Cells["Employee_PlaceOfBirth"].Value = _ds.Tables[0].Rows[i]["Employee_PlaceOfBirth"].ToString();
                dgvList.Rows[i].Cells["Branch_Name"].Value = _ds.Tables[0].Rows[i]["Branch_Name"].ToString();
                dgvList.Rows[i].Cells["Department_Name"].Value = _ds.Tables[0].Rows[i]["Department_Name"].ToString();
                dgvList.Rows[i].Cells["Position_Name"].Value = _ds.Tables[0].Rows[i]["Position_Name"].ToString();
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            string _month = cboMonth.Text;
            string _ReportText = "sinhnhattrongthang";
            frmPreviewReport f = new frmPreviewReport("Birth day in month", new string[] { _ReportText, _month, branch_ID.ToString(), department_ID.ToString() });
            f.Show(this);
        }

        private void btnWarning_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
            if (dgvList.Rows.Count > 0)
                btnPrint.Enabled = true;
            else
                btnPrint.Enabled = false;
        }

        private void btnViewEmloyee_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                int Employee_ID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                //int Index = Convert.ToInt32(drgvr.Cells["RowNumber"].Value.ToString());
                
                Form f = new frmEmployeeInfo(Employee_ID, 0, 0, UIParamater.Employee_Active, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    branch_ID = 0;
                    department_ID = 0;
                }
                else
                    branch_ID = Program.intBranchID;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();

            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void frmBirthDayList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                //dgvList.Columns["Employee_Code"].HeaderText = UpdateUI.GetString("manhanvien");
                dgvList.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvList.Columns["Employee_DateOfBirth"].HeaderText = UpdateUI.GetString("ngaysinh");
                dgvList.Columns["Employee_PlaceOfBirth"].HeaderText = UpdateUI.GetString("noisinh");
                dgvList.Columns["Employee_Sex"].HeaderText = UpdateUI.GetString("gioitinh");
                dgvList.Columns["Employee_Age"].HeaderText = UpdateUI.GetString("tuoi");
                dgvList.Columns["Branch_Name"].HeaderText = UpdateUI.GetString("chinhanh");
                dgvList.Columns["Department_Name"].HeaderText = UpdateUI.GetString("phongban");
                dgvList.Columns["Position_Name"].HeaderText = UpdateUI.GetString("chucvu");
            }
        }

        private void frmBirthDayList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfEmployee.Close();
            wcfMain.Close();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnViewEmloyee_Click(null, null);

        }
    }
}
